#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
#include<limits.h>

enum State {
	VAILD,
	INVAILD
}state=INVAILD;

int My_atoi(char* str) {
	assert(str != NULL);
	if (*str == '\0')
		return 0;
	while (isspace(*str))
		str++;
	int flag = 1;
	if (*str == '+') {
		flag = 1;
		str++;
	}
	else if (*str == '-') {
		flag = -1;
		str++;
	}
	long long ret =0;
	while (*str != '\0') {
		if (isdigit(*str)) {
			ret = ret * 10 + (*str - '0') * flag;
			if (ret > INT_MAX)
				return INT_MAX;
			else if (ret < INT_MIN)
				return INT_MIN;
		}
		else
			return (int)ret;
		str++;
	}
	if (*str == '\0')
		state = VAILD;
	return (int)ret;
}

int main() {
	int ret = My_atoi("  -123");
	if (state==VAILD)
		printf("合法转化：%d\n", ret);
	else
		printf("非法转化：%d\n", ret);
}